package com.banshuibao.bsb_android.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.pictureUtils.FullyGridLayoutManager;
import com.banshuibao.bsb_android.pictureUtils.GridImageAdapter;
import com.luck.picture.lib.model.FunctionConfig;
import com.luck.picture.lib.model.LocalMediaLoader;
import com.luck.picture.lib.model.PictureConfig;
import com.yalantis.ucrop.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangpan on 2017/12/15.
 */

public class CameraUtil {
    Context context;
    GridImageAdapter adapter1;
    private int selectMode = FunctionConfig.MODE_MULTIPLE;
    private int maxSelectNum = 3;// 图片最大可选数量
    private boolean isShow = true;
    private int selectType = LocalMediaLoader.TYPE_IMAGE;
    private int copyMode = FunctionConfig.CROP_MODEL_DEFAULT;
    private boolean enablePreview = true;
    private boolean isPreviewVideo = true;
    private boolean enableCrop = true;
    private boolean theme = false;
    private boolean selectImageType = false;
    private int compressW = 0;
    private int compressH = 0;
    private boolean isCompress = true;
    private boolean isCheckNumMode = false;
    private int compressFlag = 2;// 1 系统自带压缩 2 luban压缩
    private List<LocalMedia> selectMedia = new ArrayList<>();
    private OnClickOkListener onClickOkListener;

    public CameraUtil(Context context, int count) {
        this.context = context;
        maxSelectNum = count;
    }

    public void setLayout(RecyclerView recyclerView) {
        if (recyclerView != null) {
            FullyGridLayoutManager manager = new FullyGridLayoutManager(context, 4, GridLayoutManager.VERTICAL, false);
            recyclerView.setLayoutManager(manager);
            adapter1 = new GridImageAdapter(context, onAddPicClickListener);
            adapter1.setSelectMax(maxSelectNum);
            recyclerView.setAdapter(adapter1);

            adapter1.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int position, View v) {
                    // 这里可预览图片
                    PictureConfig.getPictureConfig().externalPicturePreview(context, position, selectMedia);
                }
            });
        }
    }


    /**
     * 删除图片回调接口
     */
    private GridImageAdapter.onAddPicClickListener onAddPicClickListener = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick(int type, int position) {
            switch (type) {
                case 0:
                    if (!isOutListener)
                        goPhoto();
                    break;
                case 1:
                    // 删除图片
                    selectMedia.remove(position);
                    //删除对应图片路径
                    adapter1.notifyItemRemoved(position);
                    setData();
                    break;
            }
        }
    };

    private void goPhoto() {
        // 进入相册
        /**
         * type --> 1图片 or 2视频
         * copyMode -->裁剪比例，默认、1:1、3:4、3:2、16:9
         * maxSelectNum --> 可选择图片的数量
         * selectMode         --> 单选 or 多选
         * isShow       --> 是否显示拍照选项 这里自动根据type 启动拍照或录视频
         * isPreview    --> 是否打开预览选项
         * isCrop       --> 是否打开剪切选项
         * isPreviewVideo -->是否预览视频(播放) mode or 多选有效
         * ThemeStyle -->主题颜色
         * CheckedBoxDrawable -->图片勾选样式
         * cropW-->裁剪宽度 值不能小于100  如果值大于图片原始宽高 将返回原图大小
         * cropH-->裁剪高度 值不能小于100
         * isCompress -->是否压缩图片
         * setEnablePixelCompress 是否启用像素压缩
         * setEnableQualityCompress 是否启用质量压缩
         * setRecordVideoSecond 录视频的秒数，默认不限制
         * setRecordVideoDefinition 视频清晰度  Constants.HIGH 清晰  Constants.ORDINARY 低质量
         * setImageSpanCount -->每行显示个数
         * setCheckNumMode 是否显示QQ选择风格(带数字效果)
         * setPreviewColor 预览文字颜色
         * setCompleteColor 完成文字颜色
         * setPreviewBottomBgColor 预览界面底部背景色
         * setBottomBgColor 选择图片页面底部背景色
         * setCompressQuality 设置裁剪质量，默认无损裁剪
         * setSelectMedia 已选择的图片
         * setCompressFlag 1为系统自带压缩  2为第三方luban压缩
         * 注意-->type为2时 设置isPreview or isCrop 无效
         * 注意：Options可以为空，默认标准模式
         */

        int selector = R.drawable.select_cb;
        FunctionConfig config = new FunctionConfig();
        config.setType(selectType);
        config.setCopyMode(copyMode);
        config.setCompress(isCompress);
        config.setEnablePixelCompress(true);
        config.setEnableQualityCompress(true);
        config.setMaxSelectNum(maxSelectNum);
        config.setSelectMode(selectMode);
        config.setShowCamera(isShow);
        config.setEnablePreview(enablePreview);
        config.setEnableCrop(enableCrop);
        config.setPreviewVideo(isPreviewVideo);
        config.setRecordVideoDefinition(FunctionConfig.HIGH);// 视频清晰度
        config.setRecordVideoSecond(60);// 视频秒数
        //  config.setCropW(cropW);
        //  config.setCropH(cropH);
        config.setCheckNumMode(isCheckNumMode);
        config.setCompressQuality(100);
        config.setImageSpanCount(4);
        config.setSelectMedia(selectMedia);
        config.setCompressFlag(compressFlag);
        config.setCompressW(compressW);
        config.setCompressH(compressH);
        if (theme) {
            config.setThemeStyle(ContextCompat.getColor(context, R.color.blue));
            // 可以自定义底部 预览 完成 文字的颜色和背景色
            if (!isCheckNumMode) {
                // QQ 风格模式下 这里自己搭配颜色，使用蓝色可能会不好看
                config.setPreviewColor(ContextCompat.getColor(context, R.color.white));
                config.setCompleteColor(ContextCompat.getColor(context, R.color.white));
                config.setPreviewBottomBgColor(ContextCompat.getColor(context, R.color.blue));
                config.setBottomBgColor(ContextCompat.getColor(context, R.color.blue));
            }
        }
        if (selectImageType) {
            config.setCheckedBoxDrawable(selector);
        }

        // 先初始化参数配置，在启动相册
        PictureConfig.init(config);
        PictureConfig.getPictureConfig().openPhoto(context, resultCallback);
    }

    private boolean isOutListener;

    public void goPhoto(boolean b) {
        isOutListener = true;
        goPhoto();
    }

    /**
     * 图片回调方法
     */
    private PictureConfig.OnSelectResultCallback resultCallback = new PictureConfig.OnSelectResultCallback() {
        @Override
        public void onSelectSuccess(List<LocalMedia> resultList) {
            selectMedia = resultList;
            setData();
        }
    };


    public interface OnClickOkListener {
        void gobackData(List<String> list);
    }

    public void setOnClickOkListener(OnClickOkListener listener) {
        this.onClickOkListener = listener;
    }

    public void setData() {
        if (selectMedia != null) {
            adapter1.setList(selectMedia);
            adapter1.notifyDataSetChanged();
            List<String> list = new ArrayList<>();
            list.clear();
            for (int i = 0; i < selectMedia.size(); i++) {
                String path = Base64ForImg.imgToBase64(selectMedia.get(i).getCutPath());//得到裁剪后的路径
                Bitmap b = Base64ForImg.stringtoBitmap(path);
                list.add(path);
            }
            if (onClickOkListener != null) {
                onClickOkListener.gobackData(list);
            }
        }
    }

    public void clear() {
        if (selectMedia != null) {
            selectMedia.clear();
            setData();
        }
    }
}
